
#ifndef _WEBSPIDERCUSTOM_H_
#define _WEBSPIDERCUSTOM_H_

#include "../activite/webspideraround.h"
#include "communkit.h"

#include "../../third/log4qt/src/log4qt/logger.h"
#include "../../third/log4qt/src/log4qt/basicconfigurator.h"
#include "../../third/log4qt/src/log4qt/propertyconfigurator.h"

class Operator;
class WebSpider : public CommunKit {
    public:
    WebSpider(ProcessRouter::Channel *channel);
    ~WebSpider();

    void removeSuccessOpt(Operator *opt);
    bool parse(Operator *opt);
    bool rawPageError(Operator *opt);
    bool sendDownloadInfo(Operator *opt);

    QString name() {return m_name;}
    void setName(QString name);
    void destroy();
    void taskLoop();

    private:
    // All remote call function.
    void loadUrl(std::string clientid, QList<std::string> &argstrs);
    void setHtml(std::string clientid, QList<std::string> &argstrs);
    void setSetting(std::string clientid, QList<std::string> &argstrs);
    void get(std::string clientid, QList<std::string> &argstrs);
    void destroy(std::string clientid, QList<std::string> &argstrs);
    void setDatabaseEnabled(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getTitle(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void renderTreeDump(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getPageSource(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getUrl(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getElementById(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void setCookies(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void cookies(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void setFilterRule(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void setCookiesWithUrl(std::string clientid, QList<std::string> &argstrs);
    void deleteCookie(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getElementsByTagName(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getElementsByName(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getElementsByXpath(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getElementByCssSelector(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getElementsByCssSelector(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getTagName(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getAttribute(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getProperty(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getText(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getValue(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void type(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void sendKeys(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void setValue(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void goBack(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void goForward(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void refresh(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void submit(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void click(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void toggle(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void focus(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void selected(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void setSelected(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void checked(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void setChecked(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void selectOptionByIndex(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void stale(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void activeElement(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void selectFrameByName(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void selectFrameByIdx(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void isVisible(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void drag(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getRect(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getAlertText(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getInnerXml(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getOuterXml(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void setViewportSize(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getStateInfo(std::string clientid, QList<std::string> &argstrs);
    void checkAvailable(std::string clientid, QList<std::string> &argstrs);
    void renderBase64(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void setDestroyTime(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void scroll(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void setAttribute(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void removeAttribute(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getChildsByCssSelector(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void getDomTreeSource(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void postData(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void method(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void download(Operator *opt, std::string clientid, QList<std::string> &argstrs);
    void mouseMove(Operator *opt, std::string clientid, QList<std::string> &argstrs);

    // Tool function
    QString arrayToJson(QList<long> arr);
    void jsonToList(QString jsonString, QList<QString> &result);
	void jsonStringToQMap(QString jsonString, QMap<QString, QString> &map);
    QString mapToJsonString(QMap<QString, QString> &map);
    void parseAndCallFunc(QString message);
    void jsonToVariantMapList(QString jsonStr, QVariantList &variant);
    QString variantMapListToJson(QVariantList var);
    QString getDownloadInfoJson(WebSpiderDownloadRequest *req);

    QString m_name;
    int m_itemsCount;
    QList<Operator*> m_successOpt;
    bool m_killed;
    QMap<WebSpiderRequest *, QString> m_getReqMap;
    QMap<long, QString> m_syncRawPageLoadMap;
    QMap<Operator *, QString> m_syncLoadMap;
    Log4Qt::Logger *m_logger;
};

#endif

